/**
 * 房間中撲克牌视图控制
 * 
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-2-8 13:32
 */
package com.addcn.texas.view
{

	import com.addcn.texas.view.components.Poker;
	import flash.display.*;
	import flash.events.Event;
	import flash.display.Stage;		
	import flash.events.*;
	import flash.utils.Timer;
	
	import com.addcn.fog.view.*;
	import com.addcn.fog.control.*;
	import com.addcn.fog.model.*;
	
	import com.addcn.texas.Main;
	import com.addcn.texas.control.*
	import com.addcn.texas.view.*;
	import com.addcn.texas.model.*;	
	import com.addcn.texas.utils.Chips;
	import com.addcn.texas.utils.Coordinate;
	import com.addcn.common.utils.AppLibary;
	import com.addcn.common.utils.RemoveAllChildren;
	
	import caurina.transitions.Tweener;
		
	public class GameView extends View
	{				
		public  static const POKER_RESULT : String = "pokerResult";
		public  static const CLEAR_POKER : String = "clearPoker";
		public static const SHOW_POKER_RESULT : String = "showPokerResult"
		
		private var _container : Object;
		private var _components : Object;
		private var _app : AppLibary 
		private var _class : Class;
		private var _poker_class :Class;
		private var _card_back : Class;
		private var _public_poker : int;  //已經顯示的公共牌數		
		private var _cards_pos : Array;   //桌面上扑克牌位置
		private var _poker_pos : Array;   //玩家扑克牌位置
		private var _table_cards : int;  //已发给玩家牌数
		private var _my_sit : int;       //玩家自己的座位號
		private var _my_poker : Array;   //玩家自己的撲克牌
		private var _win_poker : Array   //二維數組,不同池底的贏家成手牌
		private var _all_poker : Object  //所有已顯示的撲克牌
		private var _pool : Array;       //池底
		
		public function GameView(r_target : Object) {
			_container = r_target;	
			_intialize();
		}			
		
		/**
		 * 該視圖中介器需要偵聽的通知
		 */
		override public function notificationsList():void 
		{
			notifications = [
								GameInfoModel.GAME_DATA_REDY,	
								GameInfoModel.DEAL_COMMUNITY_CARDS,
								GameInfoModel.DEAL_USER_POKER,
								//RoomUserModel.USER_BET,
								SHOW_POKER_RESULT,
								RoomUserModel.USER_STANDUP,
								POKER_RESULT,
								CLEAR_POKER,
							];
		}
		
		override public function onDataChanged(r_notification:String, r_data:Object = null):void {
			switch (r_notification) {
				//case GameInfoModel.GAME_DATA_REDY: 			
					//
					//break;		
				//case GameInfoModel.DEAL_COMMUNITY_CARDS: 			
					//_setCommunityCards(r_data);
					//break;		
				//case GameInfoModel.DEAL_USER_POKER:
					//var _sit_list = r_data["sitList"];
					//_my_sit = r_data["sit"] != null ? r_data["sit"] : 0 ;
					//_my_poker = r_data["poker"] != null  ? r_data["poker"] : [];
					//_setTableCards(_sit_list);					
					//break;
				//case RoomUserModel.USER_BET:
//
					//_showChip(r_data);
					//break;				
				//case SHOW_POKER_RESULT:					
					//for each(var _val in r_data) {
						//if (_my_sit != _val["sit"]){;
							//_showPoker(_val["userPoker"][0],_val["sit"]);
							//_showPoker(_val["userPoker"][1], _val["sit"]);
						//}	
						//this._win_poker.push(_val["winPoker"]);					
					//}										
					//break;
				//case RoomUserModel.USER_STANDUP:
					//var arr : Array = [r_data['sit']];
					//_clearCards(arr);
					//_clearTableCards(arr);					
					//break;					
				//case POKER_RESULT:
					//MonsterDebugger.trace("POKER_RESULT", r_data);
					//var _sit : int = r_data["sit"]; 
					//if (_my_sit != _sit){;
						//_showPoker(r_data["userPoker"][0],_sit);
						//_showPoker(r_data["userPoker"][1],_sit);
					//}
					//_showMadeHand(r_data["winPoker"]);
					//break;
				//case CLEAR_POKER:
					//this._clearPoker();
					//break;
			}
		}
		
		private function _intialize() : void
		{		
			//建立容器
			var _sprite = new Sprite;
			_container.addChild(_sprite);
			_container = _sprite;	
			
			//獲取撲克牌類
			_app = AppLibary.getInstance();	
			this._poker_class = _app.getClass("com.addcn.texas.view.components.Poker");
			this._card_back = _app.getClass("cardBack");
			
			this._public_poker = 0;
			this._table_cards = 0
			this._win_poker = [];
			this._all_poker = { };
			this._pool = [];
			_components = { };
			
			//撲克坐標
			var _coord : Coordinate = Coordinate.getInstance();
			_cards_pos = _coord.CardCoord();
			_poker_pos = _coord.PokerCoord();
		}
				
		
		//设置公共牌
		private function _setCommunityCards(r_data : Object) : void
		{
			if (_poker_class) {
				var _val : Array;
				for each(_val in r_data) {		
					this._showPoker(_val);													
				}				
			}			
		}
		
		/**
		 * 显示扑克
		 * @param	r_data Array  [牌型,花色] 
		 */
		private function _showPoker(r_data : Array,r_sit : int = 0) : void
		{		
			var _obj_arr : Array = [];
			var _index : String;
			var _position : Object;		
			var _x : int;
			var _y : int;
			if (r_sit == 0) { //公共牌
				_public_poker++
				if (_public_poker > 5) {
					_public_poker = 1;
				}
				_index = "public";
				_obj_arr = this._components[_index] == null ? [] : this._components[_index] ;
				_x = 270+(_public_poker-1)*45;
				_y = 155;							
			}else if(r_sit >0 ){//私有牌
				_index = "poker" + r_sit;
				_obj_arr = this._components[_index] == null ? [] : this._components[_index] ;
				_position = this._poker_pos[r_sit];
				if (!_position) return;
				_x = _position["x"];
				_y = _position["y"];	
				if (_obj_arr.length == 1 ) {				
					_x += 46;								
				}
				
			}else {
				return;
			}
			var _mc = new _poker_class;
			_mc.initialize(r_data);
			_container.addChild(_mc);
			_mc.x = _x;
			_mc.y = _y;				
			_obj_arr = _obj_arr.concat(_mc);
			this._components[_index] = _obj_arr;
			this._all_poker[String(r_data[0]).toLocaleUpperCase() + r_data[1]] = _mc ;			
			
		}
		
		/**
		 * 设置桌面玩家私有牌
		 * @param	r_data  [座位号,...]
		 */
		private function _setTableCards(r_data : Object) : void
		{
			if (_card_back) {
				var _i = 0;
				var _deal_num : int = 2;
				var _cards : Array ;
				for (; _deal_num > 0 ; _deal_num--){ 
					for each(var _val in r_data) {
						_cards = this._components["cards" + _val];
						var _mc = new _card_back;
						_mc.alpha = 0;
						_mc.x = 376;
						_mc.y = 141;
						_mc.rotation = this._cards_pos[_val].r;
						if(_deal_num == 1){
							_mc.rotation -= 15;
						}
						this._container.addChild(_mc);					
						this._components["cards" + _val] = _cards != null ? _cards.concat(_mc): [_mc];								
						var _option : Object = [];
						_option["x"] = this._cards_pos[_val].x;
						_option["y"] = this._cards_pos[_val].y;
						_option["time"] = 0.5;
						_option["delay"] = _i*0.2;					
						_option["onStart"] = _tweenStart;
						_option["onStartParams"] = [_mc];
						_option["onComplete"] = _tweenComplete;
						_option["transition"] = "easeOutQuart";				
						_option["onCompleteParams"] = [_mc, (2-_deal_num), _val];				
						Tweener.addTween(_mc, _option );
						_i++;
					}
				}				
			}			
		}
		
		private function _tweenStart(r_obj : Object) : void
		{
			r_obj.alpha = 1;
		}
		
		private function _tweenComplete(r_obj : Object,r_deal_num : int, r_sit : int) : void {			
			Tweener.removeTweens(r_obj, "x", "y");	
			if (r_sit == this._my_sit) {
				var _position : Object = this._poker_pos[r_sit];
				var _poker : Array = [];
				_poker = _my_poker[r_deal_num];				
				_showPoker(_poker, r_sit);				
			}
		}
		
		/**
		 * 清除桌面上反面的撲克牌
		 * @param	r_data array [座位號,...]
		 */
		private function _clearTableCards(r_data : Array) : void
		{
			var _val : String;
			var _arr : Array;
			for each(_val in r_data) {
				_arr = this._components["cards" + _val];
				if (_arr!= null) {
					for each(var _obj in _arr) {
						if (_obj != null) {
							_obj.parent.removeChild(_obj);
						}
					}
				}
				this._components["cards" + _val] = null;								
			}
		}
		
		/**
		 * 清除某個座位正面的扑克牌
		 * @param	r_data [座位号,...]
		 */
		private function _clearCards(r_data : Array) : void
		{			
			var _val : String;
			var _arr : Array;
			for each(_val in r_data) {				
				_arr = this._components["poker" + _val];
				if (_arr!= null) {
					for each(var _obj in _arr) {
						if (_obj != null && _obj.parent != null) {
							_obj.parent.removeChild(_obj);
						}
					}
				}
				this._components["poker" + _val] = null;
			}
		}
		
		/**
		 * 清除公共牌
		 */
		public function _clearPublicPoker() : void
		{			
			var _obj : Object;
			for each(_obj in this._components["public"]) {								
				if (_obj != null) { 	
					if (_obj.parent == null) continue;
					_obj.parent.removeChild(_obj);					
					//delete this._all_poker[_key];
				}
			}
			this._components["public"] = null;	
			_public_poker = 0;
			sendNotification(RoomUserView.HIDEN_USER_STATUS);
		}
		
		///**
		 //* 顯示籌碼
		 //* @param	r_data Object
		 //*/
		//private function _showChip(r_data : Object) : void
		//{			
			//var _mc : Chips = new Chips(r_data["bet"]);
			//_mc.show(this._container);
			//_mc.x = this._cards_pos[r_data['sit']]['x'];
			//_mc.y = this._cards_pos[r_data['sit']]['y'];			
			//this._components["chip" + r_data["sit"]] = _mc;
		//}
		//
		//private function _clearChip() : void
		//{
			//var _obj: Object;
			//for (var _i = 1; _i <= 9; _i++ ) {
				//_obj = this._components["chip" + _i];
				//if(_obj != null){
					//_obj.parent.removeChild(_obj);
				//}
			//}
		//}
		
		/**
		 * 清除撲克牌
		 */
		private function _clearPoker( ) : void
		{
			var _obj : Object;
			for (var k in this._all_poker) {
				_obj = this._all_poker[k] ;
				if (_obj != null && _obj.parent != null) {					
					_obj.parent.removeChild(_obj);			
					this._all_poker[k] = null;
				}
			}
			_clearPublicPoker();
			_clearTableCards([1, 2, 3, 4, 5, 6, 7, 8, 9]);
			_clearCards([1, 2, 3, 4, 5, 6, 7, 8, 9]);
			//
			//for each(var _val in this._all_poker) {
				//if(_val != null){
					//_val.setAlpha();
				//}
			//}
			//var _obj : Object;
			//for each( _val in this._win_poker) {
				//for each(var _v in _val) {
					//_obj = this._all_poker[_v[0] + _v[1]];
					//if (_obj) {
						//_obj.setAlpha(1);
					//}
				//}
				//
			//}
			//var _clear = function () {
				//_clearPublicPoker();
				//_clearTableCards([1, 2, 3, 4, 5, 6, 7, 8, 9]);
				//_clearCards([1, 2, 3, 4, 5, 6, 7, 8, 9]);
				//timer.stop();
				//_clearChip();
				//timer.removeEventListener(TimerEvent.TIMER, _clear);
			//}
			//var timer = new Timer(6000, 1);
			//timer.addEventListener(TimerEvent.TIMER, _clear);
			//timer.start();			
		}
		
		/**
		 * 突出显示成手牌
		 * @param r_data Array [[牌型,花色],...]
		 */
		private function _showMadeHand(r_data : Array) : void
		{
			//var _obj : Object;
			//var _val : Array;			
			//for each(_obj in this._all_poker) {
				//if(_obj.parent != null && _obj != null){
					//_obj.setAlpha();
				//}
			//}			
			//MonsterDebugger.trace("_showMadeHand", r_data);
			//for each( _val in r_data) {
				//_obj = this._all_poker[String(_val[0]).toLocaleUpperCase() + _val[1]] as Poker;
				//if (_obj != null) {
					//_obj.setAlpha(1);
				//}
			//}
		}
		
		override public function destroy():void 
		{
			Tweener.resumeAllTweens();
			Tweener.removeAllTweens();
			RemoveAllChildren.remove(_container as DisplayObject);
			this._container.parent.removeChild(this._container);
		}
		
		
	
	}	
	
}
